package cn.mw.cmdb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class CustomizeThreadPool {
    public static final String ScanThreadPool = "scanThreadPool";

    public static final String BusinessProcessThreadPool = "businessProcessThreadPool";

    public static final String CloudWisdomReportThreadPool = "cloudWisdomReportThreadPool";

    public static final String SchedulThreadPool = "scheduleThreadPool";

    @Bean(BusinessProcessThreadPool)
    public Executor getThreadPoolBean(ThreadPoolConfig config) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(config.getCorePoolSize());
        // 和 ai.zixing.pool.MyDefinedThreadPool.getAsyncExecutor 基本一致
        executor.setMaxPoolSize(config.getMaxPoolSize());
        executor.setQueueCapacity(config.getQueueCapacity());
        executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 不是作为Bean的时候，需要自己手动去调用initialize()方法 不然报错
        // 可以不执行 executor.initialize(); 注入 spring 中 spring 会对该线程池初始化
//        executor.initialize();
        return executor;
    }

    @Bean(ScanThreadPool)
    public ThreadPoolTaskExecutor getScanThreadPoolBean(ScanThreadPoolConfig config) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(config.getCorePoolSize());
        // 和 ai.zixing.pool.MyDefinedThreadPool.getAsyncExecutor 基本一致
        executor.setMaxPoolSize(config.getMaxPoolSize());
        executor.setQueueCapacity(config.getQueueCapacity());
        executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 不是作为Bean的时候，需要自己手动去调用initialize()方法 不然报错
        // 参考 https://blog.csdn.net/qq_41055045/article/details/115032009
        // 可以不执行 executor.initialize(); 注入 spring 中 spring 会对该线程池初始化
//        executor.initialize();
        return executor;
    }

    @Bean(CloudWisdomReportThreadPool)
    public ThreadPoolTaskExecutor getCloudWisdomReportThreadPoolBean(ReportThreadPoolConfig config) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(config.getCorePoolSize());
        // 和 ai.zixing.pool.MyDefinedThreadPool.getAsyncExecutor 基本一致
        executor.setMaxPoolSize(config.getMaxPoolSize());
        executor.setQueueCapacity(config.getQueueCapacity());
        executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setThreadNamePrefix(config.getThreadNamePrefix());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 不是作为Bean的时候，需要自己手动去调用initialize()方法 不然报错
        // 参考 https://blog.csdn.net/qq_41055045/article/details/115032009
        // 可以不执行 executor.initialize(); 注入 spring 中 spring 会对该线程池初始化
//        executor.initialize();
        return executor;
    }

    @Bean(SchedulThreadPool)
    public ScheduledExecutorService schedulerThreadPoolBean() {
        return Executors.newScheduledThreadPool(5);
    }
}
